<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="400px" @close='close'>
    <div>
      <el-form ref="form" :model="record" label-width="70px" label-position="left" :rules="rules" style="margin:0 20px;">
        <el-form-item label="Net" prop="net">
          <template slot="label">Net
            <el-tooltip class="item" effect="light" :content="$t('提示信息:如：192.168.1.0/24')" placement="top">
              <i class="el-icon-warning-outline" style="color:#666;font-size:14px;"></i>
            </el-tooltip>
          </template>
          <el-input v-emoji v-model="record.net" ref="customNet" style="width:240px" size='small'></el-input>
        </el-form-item>
        <el-form-item label="Area" prop="area">
          <el-input v-emoji v-model="record.area" ref="customArea" style="width:240px" size='small'></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="customAddSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="customAddClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {
  addOspfNet // OSPF增加开启的网段
} from '@/api/network/route/dynamicRoute/OSPF'
import { inputClass } from '@/utils/input-class.js'
export default {
  name: 'OspfNetDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }

    return {
      dialogVisible: false,
      record: {
        net: '',
        area: ''
      },
      recordCopy: {
        net: '',
        area: ''
      },
      rules: {
        net: [{ required: false, validator: validateName, trigger: 'blur' }],
        area: [{ required: false, validator: validateName, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init () {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['customNet', 'customArea']
        inputClass(this.$refs, data)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          addOspfNet(this.record).then(res => {
            if (res.status === 0) {
              this.$messages.success(this.$t('执行成功'))
              this.close()
              this.$parent.getOspfNetListData()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    }
  }
}
</script>
